package com.his.struts.dao.question;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.note.NoteImp;
import com.his.struts.form.AnswerForm;
import com.his.struts.form.QuestionForm;
import com.his.struts.util.DbUtil;

/**
 * 提问dao
 * @author 肖伟亮
 * 用户提问
 */
public class QuestionImp implements QuestionDao {
	Logger log = Logger.getLogger(QuestionImp.class);

	DbUtil db = new DbUtil();
	
	/**
	 * 添加提问
	 * @param form 提问bean
	 */
	public void addQuestion(QuestionForm form) {
		ArrayList list = new ArrayList();
		String sql = "insert into questions(uid,qtitle,qinfo,qsex,qage,qtype,qemail,qtime) values(?,?,?,?,?,?,?,now())";
		
		list.add(form.getUid());
		list.add(form.getQtitle());
		list.add(form.getQinfo());
		list.add(form.getQsex());
		list.add(form.getQage());
		list.add(form.getQtype());
		list.add(form.getQemail());
		int n = db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	/**
	 * 我的提问
	 * @param uid 用户id
	 * @return list 我的提问
	 */
	public List<QuestionForm> getMyQuestions(int uid) {
		ArrayList list = new ArrayList();
		String sql = "select * from questions where uid =?";
		list.add(uid);
		ResultSet rs = db.executeQuery(sql, list);
		// 封装bean
		List qlist = getMyQuestions(rs);
		db.closeAll();
		return qlist;
	}

	/**
	 * 问题详情
	 * @param qid 问题id
	 * @return 问题bean
	 */
	public QuestionForm getQuestions(int qid){
		QuestionForm qform = new QuestionForm();
		String sql = "select * from questions as q inner join " +
					"userinfo as u on q.uid=u.uid and qid =?";
		ArrayList list = new ArrayList();
		list.add(qid);
		ResultSet rs = db.executeQuery(sql, list);
		try {
			if(rs.next()){
				qform.setQid(rs.getInt("qid"));
				qform.setUid(rs.getInt("uid"));
				qform.setQtitle(rs.getString("qtitle"));
				qform.setQinfo(rs.getString("qinfo"));
				qform.setQsex(rs.getString("qsex"));
				qform.setQage(rs.getInt("qage"));
				qform.setQtype(rs.getString("qtype"));
				qform.setQemail(rs.getString("qemail"));
				qform.setQtime(rs.getString("qtime"));
				qform.setUname(rs.getString("uname"));
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return qform;
	}
	
	/**
	 * 封装提问bean
	 * @param rs 提问游标
	 * @return 提问集合
	 */
	public List<QuestionForm> getMyQuestions(ResultSet rs) {
		ArrayList list = new ArrayList();
		try {
			while(rs.next()){
				QuestionForm qform = new QuestionForm();
				qform.setQid(rs.getInt("qid"));
				qform.setUid(rs.getInt("uid"));
				qform.setQtitle(rs.getString("qtitle"));
				qform.setQinfo(rs.getString("qinfo"));
				qform.setQsex(rs.getString("qsex"));
				qform.setQage(rs.getInt("qage"));
				qform.setQtype(rs.getString("qtype"));
				qform.setQemail(rs.getString("qemail"));
				qform.setQtime(rs.getString("qtime"));
				qform.setUname(rs.getString("uname"));
				list.add(qform);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}
	
	/**
	 * 删除提问
	 * @param qid 问题id
	 */
	public void deleteQuestion(int qid){
		String sql="delete from questions where qid=?";
		ArrayList list = new ArrayList();
		list.add(qid);
		db.executeUpdate(sql, list);
		sql="delete from answer where a_qid=?";
		db.executeUpdate(sql, list);
		db.closeAll();
	}
	
	
}
